package com.heima.user.service.impl;

import com.heima.user.mapper.BrandTypeMapper;
import com.heima.user.service.BrandTypeService;
import com.heima.common.exception.CustomException;
import com.heima.model.user.dtos.BrandTypeDto;
import com.heima.model.user.pojos.BrandType;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class BrandTypeServiceImpl extends ServiceImpl<BrandTypeMapper, BrandType> implements BrandTypeService {

    /**
     * 查询列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findPage(BrandTypeDto dto) {
        //1 参数检查
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 分页检查
        dto.checkParam();

        //2 执行查询
        // 分页
        Page page = new Page(dto.getPage(), dto.getSize());
        // 条件查询
        LambdaQueryWrapper<BrandType> wraaper = new LambdaQueryWrapper<>();

        // 子品牌名称
         if (StringUtils.isNotBlank(dto.getName())) {
            wraaper.like(BrandType::getName, dto.getName());
         }
        // 是否默认
         if (StringUtils.isNotBlank(dto.getIsDetail())) {
            wraaper.like(BrandType::getIsDetail, dto.getIsDetail());
         }
        // 年款
         if (StringUtils.isNotBlank(dto.getAnnual())) {
            wraaper.like(BrandType::getAnnual, dto.getAnnual());
         }
        // 排量
         if (StringUtils.isNotBlank(dto.getDisplacement())) {
            wraaper.like(BrandType::getDisplacement, dto.getDisplacement());
         }

        // 级别
         if (dto.getLevel() != null) {
            wraaper.eq(BrandType::getLevel, dto.getLevel());
         }



        IPage pageResult = page(page, wraaper);

        //3 封装分页结果
        return PageResponseResult.page(dto.getPage(), dto.getSize(),
                (int) pageResult.getTotal(),pageResult.getRecords());
    }

    /**
     * 新增
     * @param brandType
     * @return
     */
    @Override
    public ResponseResult insert(BrandType brandType) {
        //1 参数检查
        if (brandType == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 执行保存
        save(brandType);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(BrandType brandType) {

        //1.检查参数
        if(null == brandType){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.修改
        updateById(brandType);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Long id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
